# -*- coding: utf-8 -*-
from wikicamp.worklist.models import islistesi,isturu,cihazlar,bakimtablosu
from django import template
import datetime

register = template.Library()
 
@register.filter("dotsifempty")
def dotsifempty(url):
    try:
        url=unicode(url).encode("utf-8").strip()
        xlen = len(url)
        if url =="None":
            xreturn = ".."
        else:
            if xlen <= 1:
                xreturn =".."
            else:
                xreturn= url
    except Exception, e: 
        xreturn = u"- ("+unicode(e).encode("utf-8")+")" 
        
    return xreturn

@register.filter()
def setrange(xsay):
    return range(xsay)

@register.filter()
def nextint(xsayfa):
    xsayfa = int(xsayfa)
    return xsayfa+1

@register.filter()
def prevint(xsayfa):
    xsayfa = int(xsayfa)
    return xsayfa-1

@register.filter()
def getkalibay(xyil,arg):
    try:
        return islistesi.objects.filter(cihaz=cihazlar.objects.filter(id=arg),turu= isturu.objects.filter(id=6),planlanan_baslangic_tarihi__year=xyil).order_by('planlanan_baslangic_tarihi')[0].planlanan_baslangic_tarihi.month
    except:
        return "-"
@register.filter()
def getbakay(xyil,arg):
    try:
        return islistesi.objects.filter(cihaz=cihazlar.objects.filter(id=arg),turu= isturu.objects.filter(id=4),planlanan_baslangic_tarihi__year=xyil).order_by('planlanan_baslangic_tarihi')[0].planlanan_baslangic_tarihi.month
    except:
        return "-"

@register.filter()  
def getbakimtablo(xid):
    try:
        xsay = bakimtablosu.objects.filter(islistesi=islistesi.objects.get(id=xid)).count()
        if xsay > 0 :
            return True
        else:
            return False
    except:
        return False

@register.filter()  
def getbakimtablo_id(xid):
    try:
        return bakimtablosu.objects.get(islistesi=islistesi.objects.get(id=xid)).id        
    except:
        return 0
    